<?php
header ('Content-type: text/html; charset=utf-8');
if (!defined('IN_ADMIN')) {
    exit('Access Denied');
}

class Ability {
    public $mpurl;
    public $apiurl;
    
    public function __construct($apiurl) {
        $this->mpurl   = 'admin.php?ac=ability';
        $this->apiurl  = $apiurl;
        $this->apis       = array(
            'addVerifiedUser','setVerifiedUser','addAbility','setAbility','getAbilities','getAbilityNum'
        );
    }
    
    public function execute ($action) {
        if (empty($action)) return false;
        if (!in_array($action, $this->apis)) return false;
    
        $result = $this->$action(request());
        return $result;
    }
    
    /**
     * 添加大V用户
     * @param unknown $req
     * @return unknown
     */
    private function addVerifiedUser ($req) {
        $param  = array('uid' => intval($req['uid']), 'verified' => 1);
        $result = $this->requestApi('addVerifiedUser', $param);
        if ($result) {
            echo "<script language='javascript'>window.history.back(-1);</script>";
        }
        exit;
        return $result;
    }
    
    /**
     * 修改大V用户认证
     * @param unknown $req
     * @return unknown
     */
    private function setVerifiedUser ($req) {
        $uid = intval($req['uid']);
        $verified = intval($req['verified']);
        if (!in_array($verified, array(0, 1))) $verified = 0;
        
        $param  = array('uid' => $uid, 'verified' => $verified);
        $result = $this->requestApi('setVerifiedUser', $param);
        return $result;
    }
    
    /**
     * 添加达人
     * @param unknown $req
     * @return unknown
     */
    private function addAbility ($req) {
        $param  = array(
            'uid' 		=> intval($req['uid']),
            'label'     => intval($req['label']),
            'content'   => strval($req['content']),
            'status'    => 0
        );
        
        $result = $this->requestApi('addAbility', $param);
        return $result;
    }
    
    /**
     * 修改达人信息
     * @param unknown $req
     * @return unknown
     */
    private function setAbility ($req) {
        $param  = array(
            'uid' 		=> intval($req['uid']),
            'label'     => intval($req['label']) ? intval($req['label']) : 1,
            'status'    => intval($req['status'])
        );

        if(isset($req['level'])) {
            $param['medal_id'] = intval($req['level']);
        }

        $result = $this->requestApi('setAbility', $param);
        return $result;
    }
    
    /**
     * 获取达人/大V用户列表
     * @param unknown $req
     */
    private function getAbilities ($req) {
        list($uid, $page, $status, $verified, $start, $length) = $this->assemblePageData($req);

        // 参数组装
        $param  = array(
            'uid'       => $uid,
            'status'    => $status,
            'verified'  => $verified,
            'start'     => $start,
            'length'    => $length
        );

        // 请求达人列表
        $result = $this->requestApi('getAbilities', $param);

        // 获取总达人数
        $result['totalNum'] = $this->getAbilityNum($status);
        
        // 分页处理
        $mpurl = $this->mpurl."&op=getAbilities";
        if (isset($req['status']))   $mpurl  .= '&status='.$status;
        if (isset($req['verified'])) $mpurl  .= '&verified='.$verified;
        $result['pageHtml'] = $this->assemblePageUrl($mpurl, $page, $length, $result['totalNum']);
        
        // 样式选择
        $result['style'] = $this->assembleStyleData($uid, $page, $status, $verified, $length);

        return render_bak('admin/ability', $result);
    }
    
    /**
     * 获取达人数量
     * @param unknown $status
     * @return unknown
     */
    private function getAbilityNum ($status) {
        $result = $this->requestApi('getAbilityNum', array('status' => $status));
        return $result;
    }
    
    private function assemblePageData($req) {
        $uid        = $req['uid']       ? intval($req['uid'])       : 0;
        $page       = $req['page']      ? intval($req['page'])      : 1;
        $length     = $req['length']    ? intval($req['length'])    : 20;
        $status     = $req['status']    ? intval($req['status'])    : 0;
        $verified   = $req['verified']  ? intval($req['verified'])  : 0;
        $start      = ($page-1) * $length;
        
        return array($uid, $page, $status, $verified, $start, $length);
    }
    
    private function assembleStyleData($uid, $page, $status, $verified, $length) {
        $lengthstyle   = array($length   => ' selected');
        $statusstyle   = array($status   => ' selected');
        $verifiedstyle = array($verified => ' selected');
        
        $result = array('uid' => $uid ? $uid : '', 'page' => $page, 'length' => $lengthstyle, 'status' => $statusstyle, 'verified' => $verifiedstyle);
        return $result;
    }
    
    private function assemblePageUrl ($mpurl, $page, $length, $totalNum) {
        $totalPages = ceil($totalNum / $length);
        $pageurl    = multitotal($page, $totalPages, $mpurl);
        $nextpurl   = $pageurl ['next'];
        $prevpurl   = $pageurl ['prev'];
        $mpurl      = $pageurl ['urls'];
        $firstpurl  = $pageurl ['first'];
        $lastpurl   = $pageurl ['last'];
        
        $pageHtml = "<ul class='pagination'>
                        <li class='text'><a href={$firstpurl}>首页</a></li>
                        <li class='text'><a href={$prevpurl}>上一页</a></li>
                        <li class='text'><a href=$mpurl>第 {$page} 页/共 {$totalPages} 页</a></li>
                        <li class='text'><a href={$nextpurl}>下一页</a></li>
                        <li class='text'><a href={$lastpurl}>末页</a></li>
                    </ul>";
        return $pageHtml;
    }
    
    /**
     * 请求接口
     * @param unknown $do
     * @param unknown $param
     * @return unknown
     */
    private function requestApi ($action, $param) {
        if (empty($action)) return false;
        $param['do']     = 'ability';
        $param['action'] = $action;
        $param['format'] = 'json';

        $content = get_api_content($this->apiurl, $param);
        $result  = $this->parseData($content);
    
        return $result;
    }
    
    /**
     * 解析结果
     * @param unknown $data
     * @return unknown
     */
    private function parseData ($data) {
        $result = json_decode($data, true);
        $this->getErrorMessage($result['errorNo']);
        
        return $result['resultData'];
    }
    
    /**
     * 提示错误信息
     * @param unknown $errorNo
     * @return boolean
     */
    private function getErrorMessage($errorNo){
    	if (empty($errorNo)) return true;
    	
    	echo "<script>console.error('错误码为:'".$errorNo.");</script>";
    	exit();
    }
}


$ability = new Ability($_SC ['apiurl']);
echo $ability->execute($_GET['op']);
